<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
	
	private $_view = null;
	private $_layout = null;
	private $_front = null;
	private $_config = null;
	
	protected function _initAutoload() {
		Zend_Loader_Autoloader::getInstance ()->registerNamespace ( 'Core_' )->suppressNotFoundWarnings ( true );
	}
	
	protected function _initApplication() {
		Zend_Registry::set ( 'config', $this->_config = new Zend_Config_Ini ( APPLICATION_PATH . '/configs/application.ini', 'production' ) );
		Zend_Controller_Front::getInstance ()->registerPlugin ( new Core_Controller_Plugin_Multilang () )->registerPlugin ( new Core_Controller_Plugin_AclCheck () );
	
	}
	
	// TODO: Store this to the Controller Plugin
	protected function _initL10() {
		Zend_Registry::set ( 'Zend_Locale', $locale = new Zend_Locale ( 'uk_UA' ) );
		
		Zend_Registry::set ( 'Default_Lang', array_key_exists ( $lang = $locale->getLanguage (), $lang_list = $this->_config->L10->lang->toArray () ) ? $lang : $lang = $this->_config->L10->defaultLang );
	}
	
	protected function _initAcl() {
		$this->bootstrap ( 'env' );
		$acl = new Zend_Acl ();
		
		$acl->addRole ( new Zend_Acl_Role ( Application_Model_User::GUEST ) )->addRole ( new Zend_Acl_Role ( Application_Model_User::USER ) )->addRole ( new Zend_Acl_Role ( Application_Model_User::ADMIN ) );
		
		$acl->//->add(new Zend_Acl_Resource('page.cart'))
add ( new Zend_Acl_Resource ( 'page.admin' ) );
		
		$acl->//->deny(Application_Model_User::GUEST, 'page.cart')
deny ( Application_Model_User::GUEST, 'page.admin' )->deny ( Application_Model_User::USER, 'page.admin' )->//->allow(Application_Model_User::USER, 'page.cart')
allow ( Application_Model_User::ADMIN, null );
		
		Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl ( $acl );
		
		Zend_Registry::set ( 'Zend_Acl', $acl );
		return $acl;
	}
	
	protected function _initEnv() {
		$this->bootstrap ( 'layout' );
		$this->bootstrap ( 'api' );
		$this->bootstrap ( 'user' );
		
		$api = $this->getResource ( 'api' );
		$user = $this->getResource ( 'user' );
		$menu = $this->getResource ( 'menu' );
		
		$this->_front = Zend_Controller_Front::getInstance ();
		$this->_layout = $this->getResource ( 'layout' );
		$this->_view = $this->_layout->getView ();
		$this->_view->config = $this->_layout->config = Zend_Registry::get ( 'config' );
		$this->_view->user = $user;
		$this->_view->menu = $menu;
		$this->_view->amount = $api->Cart->getAmount ();
	
	}
	
	protected function _initRoutes() {
		$router = $this->_front->getRouter ();
		$router->removeDefaultRoutes ();
		
		$chainRouter = new Core_Controller_Router_Route_ChainFix ();
		$langRouter = new Core_Controller_Router_Route_Lang ( 'lang', Zend_Registry::get ( 'Default_Lang' ), array_keys ( $this->_config->L10->lang->toArray () ) );
		
		$dispatcher = $this->_front->getDispatcher ();
		$request = $this->_front->getRequest ();
		$moduleRouter = new Zend_Controller_Router_Route_Module ( array (), $dispatcher, $request );
		
		$chainRouter->chain ( $langRouter )->chain ( $moduleRouter );
		
		$router->addRoute ( 'default', $chainRouter );
	}
	/*
	protected function _initMenu() 
	{
		$this->bootstrap('user');
		$user = $this->getResource('user');
		require_once APPLICATION_PATH . '/configs/menu.php';
		Zend_Registry::set('menu', $nav = new Application_Model_Menu($MainMenu));
		if($user->isAdmin()) $nav->addPage('admin', array('label' => _('main-menu admin'), 'controller' => 'admin', 'action' => 'read'));		
		return $nav;	
	}
	*/
	protected function _initNavigation() {
		require_once APPLICATION_PATH . '/configs/menu.php';
		Zend_Registry::set ( 'Zend_Navigation', $nav = new Zend_Navigation ( $MainMenu ) );
		
		$this->bootstrap ( 'api' );
		$api = $this->getResource ( 'api' );
		
		$catalog = $nav->findOneBy ( 'id', 'catalog' );
		
		foreach ( $api->Category->getTopLevel () as $category ) {
			$catalog->addPage ( new Zend_Navigation_Page_Uri ( array ("label" => $category->title, "id" => $category->id, "uri" => $category->getUrl () ) ) );
		}
		
		$author = $nav->findOneBy ( 'id', 'author' );

        $author->addPage(new Zend_Navigation_Page_Uri (  array ("label" => 'Всі майстри', "id" => 0, "uri" => '/author' ) ) );//array( 'label' => 'Всі майстри', 'controller' => 'author', 'action' => 'index', 'params' => array('prof' => 0) ) ) );		
		
	    foreach ($api->Profession->getAll() as $profession) {
            $author->addPage(new Zend_Navigation_Page_Uri ( array ("label" => $profession->plurality, "id" => $profession->id, "uri" => $profession->FilterUrl () ) ) );
        }
		
		return $nav;
	}
	
	protected function _initPage() {
		$this->_view->headTitle ( 'Зала' );
		
		$this->_view->headMeta ()->appendHttpEquiv ( 'Content-Type', 'text/html; charset=UTF-8' );
		//TODO: meta keywords!
		

		$this->_view->headLink ()->prependStylesheet ( '/styles/basic_ie.css' )->prependStylesheet ( '/styles/basic.css' )->appendStylesheet ( '/styles/busket.css' )->appendStylesheet ( '/styles/main.css' );
		
		$this->_view->headScript ()->prependFile ( '/scripts/jquery-1.4.4.min.js' )->appendFile ( '/scripts/jquery.jqmodal.min.js' )->appendFile ( '/scripts/addToCart.js' )->appendFile ( '/scripts/cart.js' )->appendFile ( '/scripts/global.js' )->appendFile ( '/scripts/call.js' );
		
		$this->_view->doctype ( 'XHTML1_TRANSITIONAL' );
	}
	
	protected function _initAuth() {
		$this->bootstrap ( 'Db' );
		$db = $this->getResource ( 'Db' );
		
		Zend_Registry::set ( 'Zend_Auth', $adapter = new Zend_Auth_Adapter_DbTable ( $db, 'users', 'login', 'password', 'MD5(?)' ) );
		
		return $adapter;
	}
	
	protected function _initUser() {
		$user = new Application_Model_User ();
		if ($data = Zend_Auth::getInstance ()->getIdentity ()) {
			$user->setLogin ( $data->login );
			$user->setPriceScheme ( $data->price_scheme );
			if ($data->is_admin)
				$user->setAdmin ();
		}
		Zend_Registry::set ( 'Core_User', $user );
		$this->_view->user = $user;
		Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole ( $user->getRole () );
		return $user;
	}
	
	protected function _initLoader() {
		$loader = new Zend_Loader_Autoloader_Resource ( array ('basePath' => APPLICATION_PATH . '/models', 'namespace' => 'Api' ) );
		
		$loader->addResourceType ( 'dbtable', 'DbTable', 'DbTable' )->addResourceType ( 'select', 'Select', 'Select' )->addResourceType ( 'form', 'Form', 'Form' )->addResourceType ( 'row', 'Row', 'Row' );
		
		$common = new Zend_Application_Module_Autoloader ( array ('namespace' => 'Common', 'basePath' => APPLICATION_PATH . '/../library/Core' ) );
	}
	
	protected function _initApi() {
		$this->bootstrap ( 'loader' );
		$this->bootstrap ( 'db' );
		Zend_Registry::set ( 'api', $api = Application_Model_Manager::getInstance () );
		return $api;
	}
}